package com.fruit.alisms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fruit.pojo.sms.SmsTemplet;

/**
 * 阿里短信
 * 
 * @ClassName: AliSms
 * @author 葛欢欢
 * @version 2017.11.30 16:40
 */
public final class AliSms {
	private AliSms() {
		
	}

	private static final String PRODUCT = "Dysmsapi";
	private static Logger logger = LoggerFactory.getLogger(AliSms.class);

	/**
	 * 发送短信
	* @author 葛欢欢
	* @version 2017年12月13日 
	* @param request request
	* @param st st
	* @return String
	 */
	public static String sendCode(SendSmsRequest request, SmsTemplet st) {
		SendSmsResponse sendSmsResponse = null;
		String result = "";
		try {
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", st.getAppKey(), st.getAppSecret());
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, st.getSendUrl());
			IAcsClient acsClient = new DefaultAcsClient(profile);
			request.setMethod(MethodType.POST);
			request.setSignName(st.getSign());
			request.setTemplateCode(st.getTempletId());
			sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
				result = "success";
			} else {
				result = "error";
			}
		} catch (ClientException e) {
			e.printStackTrace();
			logger.info("发送短信---【异常】---" + e.getMessage());
			result = "error";
		}
		return result;
	}
}
